package com.epi.one.main;

import com.epi.one.basic.exception.MyRunTimeException;
import com.epi.one.basic.log.WinHandler;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.*;

/**
 * @author dxy
 * @version 1.0
 */
public class InterDemo {
    public static void main(String[] args)  {
        test();
    }

    public static void test()  {
        innerAnonymityClass();
        checkIndexParam();
        logLevel(2,4);
        addSupressed();
        addExCause();
    }

    private static void logLevel(int a, int b) {
        Logger log = Logger.getGlobal();
        log.setLevel(Level.ALL);

//        var handler = new ConsoleHandler(); //自定义处理器
//        var handler = new FileHandler("resourse/log.txt",true);
//        var handler = new SocketHandler("localhost", 8088);
        var handler = new WinHandler();
        handler.setLevel(Level.ALL);

        log.setUseParentHandlers(false); // 关闭默认的控制台 Handler
        log.addHandler(handler);         // 使用自定义的控制台 Handler

        log.entering(InterDemo.class.getName(), "logLevel", new Object[]{a, b});
        log.severe("SEVERE: This is an error message");
        log.warning("WARNING: This is a warning message");
        log.info("INFO: This is an info message");
        log.config("CONFIG: This is a config message");
        log.fine("FINE: This is a fine message");
        log.finer("FINER: This is a finer message");
        log.finest("FINEST: This is a finest message");
        log.exiting(InterDemo.class.getName(), "logLevel", a + b);
    }

    private static void checkIndexParam() {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        int index =10;
//        AssertionError
//        assert index >= 0 && index < arr.length;
//        ArrayIndexOutOfBoundsException
//        int i = arr[index];
//        IndexOutOfBoundsException
//        Objects.checkIndex(index,arr.length);
        System.out.println("Done");
    }

    private static void addSupressed() {
        try (
            FileInputStream fis = new FileInputStream("file.txt") {
            @Override
            public void close() throws IOException {
                super.close();
                throw new IOException("人为制造的 close 异常");
            }
        };) {
            throw new RuntimeException("主异常");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("========================");

        try {
            throw new Exception("主异常");
        } catch (Exception main) {
            Exception suppressed = new Exception("被压制的异常");
            main.addSuppressed(suppressed);
            main.printStackTrace();
        }
    }

    private static void addExCause() {
        try {
            throwEx();
        }catch (RuntimeException e ) {
            e = new RuntimeException("1",e);
            e = new RuntimeException("2",e);
            e = new RuntimeException("3",e);
//            方式一
//            e.printStackTrace(System.out);
            throw e;
        }
    }

    private static void throwEx() {
//        方式二
//        StackWalker walker = StackWalker.getInstance();
//        walker.forEach(System.out::println);
        RuntimeException e = new MyRunTimeException("根本错误");
//        方式三
//        StackTraceElement[] eStackTrace = e.getStackTrace();
        throw e;
//        throw new MyIOException();
    }

    private static void innerAnonymityClass() {
        Object a=new Object();
        Object b=new Object();
        Object c=new Object(){String name ="1";};
        System.out.println(a.getClass().equals(b.getClass()));
//        System.out.println(c.name);
        System.out.println(a.getClass().equals(c.getClass()));
//        Thread.dumpStack();
    }
}
